﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using blackhouse.mojoPortalExtend;
using blackhouse.NemoBlog.Business;
using blackhouse.NemoBlog.Business.Presenter;
using blackhouse.NemoBlog.Business.View;
using mojoPortal.Business;
using mojoPortal.Web;
using mojoPortal.Business.WebHelpers;

namespace blackhouse.NemoBlog.Web {
    public partial class NemoBlogPostPage : NonCmsBasePage {

        #region Fields

        private PostPresenter presenter;
        private PageProvider page = new PageProvider();

        #endregion

        #region Event Handlers

        protected override void OnInit(EventArgs e) {
            base.OnInit(e);

            int postId = 0;
            if (Int32.TryParse(Request["post"], out postId))
                this.PostId = postId;
        }

        protected override void OnLoad(EventArgs e) {
            
        }

        protected void Click_Remove(object sender, EventArgs e) {
            this.presenter.Remove();
        }

        protected void Click_Publish(object sender, EventArgs e) {
            this.presenter.Publish();
        }

        #endregion

        #region IPostView

        #region Properties

        public int PostId {
            get;
            private set;
        }

        public string ShortTitle {
            set;
            get;
        }

        public string PostTitle {
            set {
                this.page.SetPageTitle(value);
                this.ltlPost.Text = value;
            }
        }


        public string ShortDescription {
            set { this.page.SetPageDescription(value); }
        }

        public string PostContent {
            set { this.ltlPostContent.Text = value; }
        }

        public string PublishedDateText {
            set { this.ltlPublishDate.Text = value; }
        }

        public string IsoPublishedDateText {
            set { this.ltlIsoPublishDate.Text = value; }
        }

        public string Publisher {
            set { this.ltlPublisher.Text = value; }
        }

        public bool SimilarPostsVisible {
            set { this.pnlSimilarPosts.Visible = value; }
        }

        #endregion

        public void PopulateTags(NemoBlogTag[] tags) {
            if (tags.Length > 0) {
                this.rptTags.Visible = true;
                this.rptTags.DataSource = tags.Select(t => new DTNemoBlogTag(t));
                this.rptTags.DataBind();
            }
            else {
                this.rptTags.Visible = false;
            }
        }

        public void SetValidUrl(string postUrl) {
            string fPostUrl = postUrl.ReplaceEx("~/", "/");
            if (!this.Request.RawUrl.EndsWith(fPostUrl)) {
                this.Response.RedirectPermanent(fPostUrl);
                return;
            }
            this.page.SetCanonicalUrl(fPostUrl);
        }

        public void SetRemoveLink(int postId) {
            this.pnlActionPanel.Visible = true;
            this.phRemoveLink.Visible = true;
        }

        public void SetPublishLink(int postId) {
            this.pnlActionPanel.Visible = true;
            this.phPublishLink.Visible = true;
        }

        public void SetEditLink(NemoBlogPost post) {
            this.pnlActionPanel.Visible = true;
            this.phEditLink.Visible = true;
            this.lteEditPost.GroupSysName = post.GetGroup().SystemName;
            this.lteEditPost.RecordId = post.PostId;
            this.lteEditPost.AddSpecialParameter("pageId", CacheHelper.GetCurrentPage().PageId.ToString());
            this.lteEditPost.AddSpecialParameter("mid", post.ModuleId.ToString());
        }

        public void ShowPostLists(int placeId) {
            Module m = new Module(placeId);
            if (m.ModuleId == placeId) {
                PageSettings p = new PageSettings(m.SiteId, m.PageId);
                Response.Redirect(p.Url.ReplaceEx("~/", "/"));
            }
        }

        public void Refresh() {
            Response.Redirect(Request.RawUrl);
        }

        public void PopulateSimilarPosts(NemoBlogPost[] similarPosts) {
            this.rptSimilarPosts.DataSource = similarPosts.Select(p => new DTNemoBlogPost(p));
            this.rptSimilarPosts.DataBind();
        }

        #endregion

    }
}